﻿namespace Pibby.Core.IO
{
	using UsbEject.Library;

	public class PortableMediaEjector : IEjectDevices
	{
		public void Eject(string driveName)
		{
			char driveLetter = driveName.ToUpper(System.Globalization.CultureInfo.CurrentUICulture)[0];

			VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
			
			foreach (Volume device in volumeDeviceClass.Devices)
			{
				if (device.LogicalDrive.ToUpper(System.Globalization.CultureInfo.CurrentUICulture)[0] == driveLetter)
				{
					string result = device.Eject(true);

					if (!string.IsNullOrEmpty(result))
					{
						throw new EjectUsbDeviceException(result);
					}

					break;
				}
			}
		}
	}
}
